package name.swingeet.attach;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import com.sun.tools.attach.*;
import com.sun.tools.attach.spi.AttachProvider;
public class SwingAgent {
	static String AGENT_JAR="Swingeet.jar";
	static String AGENT_PATH = System.getProperty("user.dir") + File.separator +"lib"+File.separator+ AGENT_JAR;
	VirtualMachine vm;
	
public static void main(String args[]) throws Exception{
	SwingAgent sa=new SwingAgent(args[0],args[1]);
	
}
    public SwingAgent(String id) throws Exception {
    	
    	this(AGENT_PATH,id);
    }
    
	public SwingAgent(String agentPath,String id) throws Exception{
		
		System.out.println(agentPath);

	       VirtualMachine vm = VirtualMachine.attach(id);
	      
		try{
       vm.loadAgent(agentPath,null);
		}catch(Exception e){
			vm.detach();
			e.printStackTrace();
		}
	System.out.println("Attach to "+id+" successed!");
    
	}
	

	public void detach()throws Exception{
	
		if(vm!=null)
		vm.detach();
	}

}
